package com.maple.base.widget.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.ScreenUtils
import com.maple.utils.ext.layoutInflater
import com.maple.utils.util.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

abstract class BaseDialogFragment(val mContext: Context,
                         private val mIsCancelable: Boolean = false,
                         private val mWidth: Int = (ScreenUtils.getScreenWidth() * 0.9f).toInt(),
                         private val mHeight: Int = WindowManager.LayoutParams.WRAP_CONTENT,
                         private val mGravity: Int = Gravity.CENTER): DialogFragment(),
    CoroutineScope by MainScope() {

    abstract fun getLayoutId(): Int

    abstract fun initFragment(view: View, savedInstanceState: Bundle?)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.let {
            it.requestFeature(Window.FEATURE_NO_TITLE)
        }
        return mContext.layoutInflater.inflate(getLayoutId(),container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFragment(view, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

    override fun dismissAllowingStateLoss() {
        super.dismissAllowingStateLoss()
    }

    override fun onStart() {
        super.onStart()
        val attrs = dialog?.window?.attributes?.apply {
            this.width = mWidth
            this.height = mHeight
            this.gravity = mGravity
        }

        dialog?.setCancelable(mIsCancelable)
        /// 点击外部是否可取消
        dialog?.setCanceledOnTouchOutside(mIsCancelable)
        dialog?.window?.apply {
            setBackgroundDrawable(ColorDrawable(0))
            attributes = attrs
        }
    }


    override fun show(manager: FragmentManager, tag: String?) {
        try {
            super.show(manager, tag)
        }catch (e: Exception) {
            e.printStackTrace()
            LogUtils.logGGQ("====catch==BaseDialogFragment=>>>${e.message}")
        }
    }
    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }
}